mock

Helper function for creating a Mock object.

mock
(
T
)
(
)

Examples

1 interface Foo {
2     int foo(int, string) @safe pure;
3     void bar() @safe pure;
4 }
5 
6 int fun(Foo f) {
7     return 2 * f.foo(5, "foobar");
8 }
9 
10 auto m = mock!Foo;
11 m.expect!"foo";
12 fun(m);
1 import unit_threaded.asserts;
2 
3 interface Foo {
4     int foo(int, string) @safe pure;
5     void bar() @safe pure;
6 }
7 
8 int fun(Foo f) {
9     return 2 * f.foo(5, "foobar");
10 }
11 
12 auto m = mock!Foo;
13 m.expect!"foo"(5, "foobar");
14 fun(m);
1 interface Foo {
2     int foo(int, string) @safe pure;
3     void bar() @safe pure;
4 }
5 
6 int fun(Foo f) {
7     return 2 * f.foo(5, "foobar");
8 }
9 
10 auto m = mock!Foo;
11 fun(m);
12 m.expectCalled!"foo"(5, "foobar");
1 interface Foo {
2     int timesN(int i) @safe pure;
3 }
4 
5 int fun(Foo f) {
6     return f.timesN(3) * 2;
7 }
8 
9 auto m = mock!Foo;
10 m.returnValue!"timesN"(42);
11 immutable res = fun(m);
12 assert(res == 84);
1 interface Foo {
2     int timesN(int i) @safe pure;
3 }
4 
5 int fun(Foo f) {
6     return f.timesN(3) * 2;
7 }
8 
9 auto m = mock!Foo;
10 m.returnValue!"timesN"(42, 12);
11 assert(fun(m) == 84);
12 assert(fun(m) == 24);
13 assert(fun(m) == 0);

Meta